import datetime,time,os

print(datetime.datetime.now())              # 2023-03-21 11:27:07.542440
print(datetime.datetime.now().today())      # 2023-03-21 11:25:33.662448
print(datetime.datetime.now().ctime())      # Tue Mar 21 11:21:59 2023
print(datetime.datetime.now().date())       # 2023-03-21
print(datetime.datetime.now().time())       # 11:25:07.774940
print(datetime.datetime.now().year)         # 2023
print(datetime.datetime.now().month)        # 3
print(datetime.datetime.now().day)          # 21


print(time.time())              # 1679369352.6574771 s
print(int(time.time()))         # 1679369352
print(time.ctime())             # Tue Mar 21 14:07:06 2023
print(time.clock())             # 0.519907  linux：只计算CPU运行的时间  windows：返回该进程实际运行时间(包括sleep时间)
print(time.asctime())           # Tue Mar 21 14:40:04 2023
print(time.tzname)              # ('CST', 'CST')   返回当前时区，'CST'中国标准时间





# 时间戳转日期
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))               # 2023-03-21 15:08:24
print(datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S"))   # 2023-03-21 15:08:24


# 日期转时间戳
# 字符类型的时间
t1 = str(datetime.datetime.now())                         # 2023-03-21 11:27:07.542440
timeArray = time.strptime(t1, "%Y-%m-%d %H:%M:%S.%f")     # 转为时间数组,需要注意strptime()方法中的t1要与后面的结构对应
print(timeArray)                                          # time.struct_time(tm_year=2021, tm_mon=6, tm_mday=3, tm_hour=21, tm_min=19, tm_sec=3, tm_wday=3, tm_yday=154, tm_isdst=-1)
# timeArray可以调用tm_year等
print(timeArray.tm_year)                                  # 2023
print(timeArray.tm_mon)                                   # 3
print(timeArray.tm_wday)                                  # 1：星期：星期日-星期六（0-6）
print(timeArray.tm_yday)                                  # 80:一年当中的第80天
print(timeArray.tm_mday)                                  # 21
# 转为时间戳
timeStamp = int(time.mktime(timeArray))
print(timeStamp)                                          # 1679383404 s


# 获取指定日期的前N天日期和后N天日期
current_date = datetime.datetime.now().date()
current_datetime = datetime.datetime.now()

delta_day = datetime.timedelta(days=5)
print(current_date)              # 2023-03-21：当前天
print(current_date - delta_day)      # 2023-03-16：五天前时刻
print(current_date + delta_day)      # 2023-03-26：五天后时刻

delta_hour = datetime.timedelta(hours=5)
print(current_datetime)                     # 2023-03-21 16:45:16.296872：当前日期
print(current_datetime - delta_hour)        # 2023-03-21 11:45:16.296872：五小时前时刻
print(current_datetime + delta_hour)        # 2023-03-21 21:45:16.296872：五小时后时刻

delta_minute = datetime.timedelta(minutes=5)
print(current_datetime)                       # 2023-03-21 16:46:47.908002：当前日期
print(current_datetime - delta_minute)        # 2023-03-21 16:41:47.908002：五分钟前时刻
print(current_datetime + delta_minute)        # 2023-03-21 16:51:47.908002：五分钟后时刻


delta_ms = datetime.timedelta(milliseconds=5)
print(current_datetime)                       # 2023-03-21 16:46:47.908002：当前日期
print(current_datetime - delta_ms)            # 2023-03-21 16:41:47.908002：五毫秒前时刻
print(current_datetime + delta_ms)            # 2023-03-21 16:51:47.908002：五毫秒后时刻



base_dir = os.path.dirname(__file__)
print(base_dir)





